var app = getApp()
var url = app.url
Page({
  data: {
    items: [],//列表
    choItem: {},// 已选择
    setupflag: true,
    types: [
      { name: '全夜灯', value: 0, checked: 'true' }, { name: '半夜灯', value: 1 }, { name: '清洁照明', value: 2 }
    ],
    lightType: 0, // 全夜灯 0，半夜灯 1，清洁照明 2
    addVal: 0, // 日出调整量
    subVal: 0, // 日落调整量
    branchTypes: [],
    branch: [],
    halfLightOff: "--",
    cleanLightOn: "--"
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var idx = options.idx
    var ob = wx.getStorageSync('area' + idx)
    wx.setNavigationBarTitle({
      title: ob.name,
    })
    var array = ob.array
    this.setData({ items: array })
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  bindHalfLightOff: function (e) {
    this.setData({
      halfLightOff: e.detail.value
    })
  },
  bindCleanLightOn: function (e) {
    this.setData({
      cleanLightOn: e.detail.value
    })
  },
  bindBatchChoose: function (e) {
    var items = e.detail.value
    var temp = []
    var types = this.data.branchTypes
    if (items.length > 0) {
      if (items.indexOf("1") >= 0) {
        temp.push("1")
        for (var i = 0; i < types.length; i++) {
          if (i > 0) types[i].dis = true
        }
      } else {
        temp = items
        var types = types
        for (var i = 0; i < types.length; i++) {
          if (i > 0) types[i].dis = false
          else types[i].dis = true
        }
      }
    } else {
      for (var i = 0; i < types.length; i++) {
        types[i].dis = false
      }
    }
    console.log(temp)
    this.setData({ branch: temp, branchTypes: types })
  },
  showSetup: function () {
    var cho = this.data.choItem
    if (JSON.stringify(cho) == "{}") {
      wx.showModal({ title: '提示', content: "请选择设置终端号", showCancel: false })
      return
    } else if (cho.IS_ONLINE == 0) {
      wx.showModal({ title: '提示', content: "离线状态不能操作", showCancel: false })
      return
    }
    var types = [{ name: '一支路', value: 2 }, { name: '二支路', value: 3 }
      , { name: '三支路', value: 4 }, { name: '四支路', value: 5 }]
    var result = [{ name: '总路', value: 1 }]
    var switchs = cho.SWITCH_STATUS
    for (var i = 0; i < switchs.length; i++) {
      if (switchs[i] != "--") {
        result.push(types[i])
      }
    }
    this.setData({ setupflag: false, branchTypes: result })
  },
  hideSetup: function () {
    this.setData({ setupflag: true })
    this.setData({ branchTypes: [] })
  },
  radioChange: function (e) {
    this.setData({ lightType: e.detail.value })
  },
  bindInputadd: function (e) {
    this.setData({ addVal: e.detail.value  })
  },
  bindInputsub: function (e) {
    this.setData({ subVal: e.detail.value })
  },
  bindChoice: function (e) {
    var item = e.currentTarget.dataset.ob
    this.setData({ choItem: item })
  },
  bingToSetup: function (e) {
    var that = this
    var opy = e.currentTarget.dataset.opy
    var cho = that.data.choItem
    if (JSON.stringify(cho) == "{}") {
      wx.showModal({ title: '提示', content: "请选择设置终端号", showCancel: false })
      return
    } else if (cho.IS_ONLINE == 0){
      wx.showModal({ title: '提示', content: "离线状态不能操作", showCancel: false })
      return
    }
    var an = that.data.addVal, sn = that.data.subVal
    var header = { 'content-type': 'application/x-www-form-urlencoded' }
    var params = { 
      userName: wx.getStorageSync('userName'), meterAddr: cho.TERMINAL_ADDRESS, lightType: that.data.lightType, 
      addVal: an, subVal: sn, branch: this.data.branch.join(","), halfLightOff: this.data.halfLightOff, cleanLightOn: this.data.cleanLightOn
    }
    var encodingStr = app.ecbEncrypto(params)
    wx.showLoading({
      title: '提交中',
    })
    wx.request({
      url: url + '/xywl/setLightParams.htm',
      data: encodingStr,
      method: 'POST',
      header: header,
      success: function (res) {
        that.setData({ branchTypes: [] })
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr)
        if (rest.res == 0) {
          var respone = rest.respone
          if (respone.strBackFlag == 1){
            wx.showModal({
              title: '提示',
              content: '设置成功！',
              showCancel: false,
              success: function () {
                wx.navigateBack()
              }
            })
          }else{
            wx.showModal({
              title: '提示',
              content: '设置失败，请重试！',
              showCancel: false
            })
          }
        } else {
          wx.showModal({
            title: '提示',
            content: rest.errorMsg,
            showCancel: false
          })
        }
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },

});